<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############


class JFX_Page_File extends JFX_File
{
	public $page = null;
	public $elements = array();
	public $lang = array();
	public $subPages = array();
	public $extra = array();

	public function __construct(){
		parent::__construct();
		$langs = JFX_Lang::getInstalledLangs();
		foreach($langs as $row){
			$this->lang[$row['keyname']] = array(
				'heading' => 'Page Heading',
				'page_menu' => 'Page Menu'
			);
		}
	}

	public function setPage(JFX_Page $page){
		$this->page = $page;
		$this->subPages = JFX_Page::getSubpages($page->pageid);
	}

	public function export($filename=''){

		$hookParams = array(
			'langs' => &$this->langs,
			'page' => &$this->page,
			'extra' => &$this->extra
		);
		JFX::hook('JFX_Page_File.export', $hookParams);

		foreach($this->page->elements as $k=>$block){

			$exportFilename = $filename.'_'.$block->id.'.php';
			if($block->isBlock){
				$exportFile = new JFX_Block_File;
			}else if($block->isTemplate){
				$exportFile = new JFX_Template_File;
			}else if($block->isPage){
				$exportFile = new JFX_Page_File;
			}
			$exportFile->setBlock($block);
			$exportFile->export($exportFilename);
			$this->elements[$k] = unserialize(file_get_contents($exportFilename));
			unlink($exportFilename);
		}

		foreach($this->subPages as $k=>$subpage){
			$exportFilename = $filename.'_'.$subpage->id.'.php';
			$exportFile = new JFX_Page_File;
			$exportFile->setPage($subpage);
			$exportFile->export($exportFilename);
			$this->subPages[$k] = unserialize(file_get_contents($exportFilename));
			unlink($exportFilename);
		}

		$this->params['type'] = get_class($this);
        $this->params['title'] = $this->page->title;
        $this->params['icon_data'] = '';
        $this->params['description'] = 'Exported page '.$this->page->title;
        $this->params['created_tstamp'] = time();
        $this->params['modified_tstamps'] = array();
        $this->params['module'] = 'core';
        $this->params['method'] = 'importPage';
        $this->params['url'] = '';
        $this->params['module_minimum_version'] = '';
        $this->params['module_maximum_version'] = '';
        $this->params['core_minimum_version'] = '';
        $this->params['core_maximum_version'] = '';
        $this->params['theme_minimum_version'] = '';
        $this->params['theme_maximum_version'] = '';
		$this->params['content'] = '';
        $this->params['auto_install'] = false;

		$fh = fopen($filename, 'w');
		fwrite($fh, serialize($this));
		fclose($fh);

	}

	public function import($parentPageId, $workspaceId){
		$DB = JFX::registry('db');
		$CONFIG = JFX::registry('config');
		$USER = JFX::registry('JFX_User');

		$newPageId = $DB->insert($CONFIG->dbprefix.'pages', array('id'=>''));
		$page = new JFX_Page;
		$page->setDetails($this->page->getDetails());
		$page->setDetails(array(
			'id' => $newPageId,
			'page_id' => $newPageId,
			'parent_id' => $parentPageId
		));
		$page->save();

		$hookParams = array(
			'page' => $page,
			'data' => $this
		);
		JFX::hook('JFX_Page_File.import', $hookParams);

		

		foreach($this->elements as $k=>$blockFile){
			$blockFile->import($newPageId, $USER->workspace);
		}

		foreach($this->subPages as $pageFile){
			$pageFile->import($newPageId, $workspaceId);
		}



	}

	public static function loadFromFile($filename){
		return unserialize(file_get_contents($filename));
	}
}










